package logic

import (
	"context"
	"mall/mallrpc/internal/svc"
	"mall/mallrpc/mallrpc"
	"mall/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表
func (l *ListLogic) List(in *mallrpc.ListRequest) (*mallrpc.ListShResponse, error) {
	// todo: add your logic here and delete this line
	var list []mysql.Shop
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	
	mysql.DB.Debug().Limit(10).Offset(int(offset)).Find(&list)

	var List []*mallrpc.List
	for _, shop := range list {
		_ = mallrpc.List{
			Shopname: shop.Shopname,
			Price:    shop.Price,
			Image:    shop.Image,
		}
	}
	List = append(List)
	return &mallrpc.ListShResponse{
		List: List,
	}, nil
}
